﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WF.Model;
using WF.DAL;

namespace WF.BusinessObject
{
    /// <summary>
    /// WF_AuditHistory业务对象
    /// </summary>
    public class WF_AuditHistory : BOBase<WF_AuditHistoryEntity, DAL.WF_AuditHistory>
    {
        #region 构造函数

        public WF_AuditHistory() : base() { }
        public WF_AuditHistory(WFDBDataContext DataContext) : base(DataContext) { }
        #endregion

        #region 实现基类abstract方法
        /// <summary>
        /// 实现基类的GetById abstract方法
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public override WF_AuditHistoryEntity GetById(string ID)
        {
            var obj = DataContext.WF_AuditHistories.SingleOrDefault(o => o.RowID == ID);
            return new WF_AuditHistoryEntity(obj);
        }
        #endregion

        #region Public 方法
        public List<WF_AuditHistoryEntity> GetAll()
        {
            var target = DataContext.WF_AuditHistories.ToList();
            List<WF_AuditHistoryEntity> result = new List<WF_AuditHistoryEntity>();
            foreach (var item in target)
            {
                result.Add(new WF_AuditHistoryEntity(item));
            }
            return result;
        }
        #endregion

        #region WF_AuditHistory
        /// <summary>
        /// GetAllWF_AuditHistoryEntity
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        public bool GetAllWF_AuditHistoryEntity(out List<WF_AuditHistoryEntity> result)
        {
            var target = DataContext.WF_AuditHistories.ToList();
            result = new List<WF_AuditHistoryEntity>();
            foreach (var item in target)
            {
                result.Add(new WF_AuditHistoryEntity(item));
            }

            return true;
        }
        ///// <summary>
        ///// GetWF_AuditHistoryEntityById
        ///// </summary>
        ///// <param name="p"></param>
        ///// <param name="result"></param>
        ///// <returns></returns>
        //public bool GetWF_AuditHistoryEntityById(string p, out WF_AuditHistoryEntity result)
        //{
        //    DAL.WF_AuditHistory wfAuditHistory = DataContext.WF_AuditHistories.SingleOrDefault(obj => obj.RowID == p);
        //    result = new WF_AuditHistoryEntity(wfAuditHistory);
        //    return true;
        //}
        ///// <summary>
        ///// Add
        ///// </summary>
        ///// <param name="wF_AuditHistoryEntity"></param>
        ///// <returns></returns>
        //public bool Add(WF_AuditHistoryEntity wF_AuditHistoryEntity)
        //{
        //    DAL.WF_AuditHistory wfAuditHistory = new DAL.WF_AuditHistory();
        //    if (wF_AuditHistoryEntity.ID == null || wF_AuditHistoryEntity.ID == string.Empty)
        //        wfAuditHistory.RowID = System.Guid.NewGuid().ToString();
        //    else
        //        wfAuditHistory.RowID = wF_AuditHistoryEntity.ID;
        //    wfAuditHistory.ApplyID = wF_AuditHistoryEntity.ApplyID;
        //    wfAuditHistory.ActivityName = wF_AuditHistoryEntity.ActivityName;
        //    wfAuditHistory.EmpID = wF_AuditHistoryEntity.EmpID;
        //    wfAuditHistory.UserName = wF_AuditHistoryEntity.UserName;
        //    wfAuditHistory.ApprovalComment = wF_AuditHistoryEntity.ApprovalComment;
        //    wfAuditHistory.ApprovalResult = wF_AuditHistoryEntity.ApprovalResult;
        //    wfAuditHistory.DomainAccount = wF_AuditHistoryEntity.DomainAccount;
        //    wfAuditHistory.ApprovalDate = wF_AuditHistoryEntity.ApprovalDate;

        //    DataContext.WF_AuditHistories.InsertOnSubmit(wfAuditHistory);
        //    DataContext.SubmitChanges();
        //    return true;
        //}
        ///// <summary>
        ///// update
        ///// </summary>
        ///// <param name="wF_AuditHistoryEntity"></param>
        ///// <returns></returns>
        //public bool Update(WF_AuditHistoryEntity wF_AuditHistoryEntity)
        //{
        //    DAL.WF_AuditHistory wfAuditHistory = DataContext.WF_AuditHistories.SingleOrDefault(obj => obj.RowID == wF_AuditHistoryEntity.ID);
        //    if (wfAuditHistory == null)
        //        return false;
        //    wfAuditHistory.ApplyID = wF_AuditHistoryEntity.ApplyID;
        //    wfAuditHistory.ActivityName = wF_AuditHistoryEntity.ActivityName;
        //    wfAuditHistory.EmpID = wF_AuditHistoryEntity.EmpID;
        //    wfAuditHistory.UserName = wF_AuditHistoryEntity.UserName;
        //    wfAuditHistory.ApprovalComment = wF_AuditHistoryEntity.ApprovalComment;
        //    wfAuditHistory.ApprovalResult = wF_AuditHistoryEntity.ApprovalResult;
        //    wfAuditHistory.DomainAccount = wF_AuditHistoryEntity.DomainAccount;
        //    wfAuditHistory.ApprovalDate = wF_AuditHistoryEntity.ApprovalDate;

        //    DataContext.SubmitChanges();
        //    return true;
        //}

        ///// <summary>
        ///// delete
        ///// </summary>
        ///// <param name="p"></param>
        ///// <returns></returns>
        //public bool DeleteWF_AuditHistoryEntity(string p)
        //{
        //    DAL.WF_AuditHistory sysset = DataContext.WF_AuditHistories.SingleOrDefault(obj => obj.RowID == p);
        //    if (sysset != null)
        //    {
        //        DataContext.WF_AuditHistories.DeleteOnSubmit(sysset);
        //        DataContext.SubmitChanges();
        //    }
        //    return true;
        //}
        #endregion

        public List<WF_AuditHistoryEntity> GetAuditHistoryByBizDataID(string bizDataID)
        {
            var objs = from h in DataContext.WF_AuditHistories
                       join a in DataContext.WF_ApplyInstances
                       on h.ApplyID equals a.ApplyID
                       orderby h.ApprovalDate descending
                       where (String.IsNullOrEmpty(bizDataID) ? true : a.BizDataID == bizDataID)
                       orderby h.ApprovalDate
                       select h;

            List<WF_AuditHistoryEntity> rt = new List<WF_AuditHistoryEntity>();
            foreach (var obj in objs)
            {
                rt.Add(new WF_AuditHistoryEntity(obj));
            }
            return rt;
        }
    }
}
